package com.example.im.ui.fragment

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.example.im.R
import com.example.im.bean.UserRoom
import com.example.im.callback.OnClickRoomListener
import com.example.im.ui.adapter.RoomUserAdapter
import com.example.im.common.Constants
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.RoomActivity
import kotlinx.android.synthetic.main.fragment_room_center.*
import kotlinx.android.synthetic.main.fragment_room_center.tv_name
import kotlinx.android.synthetic.main.fragment_room_center.tv_room_name

class RoomCentreFragment : AbstractBaseFragment<BasePresenter<*>>(),RoomActivity.OnToRoomCentreFragment {

    private var userRooms = mutableListOf<UserRoom>()
    private var adapter: RoomUserAdapter? = null

    private var userRoom: UserRoom? = null

    private var mOnClickRoomListener: OnClickRoomListener? = null

    companion object {

        fun newInstance(userRoom: UserRoom): RoomCentreFragment {
            val bundle = Bundle()
            bundle.putSerializable(Constants.BUNDLE_T0_USER_ROOM_DATA, userRoom)
            val fragment = RoomCentreFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mOnClickRoomListener = context as OnClickRoomListener
    }

    override val layoutId: Int
        get() = R.layout.fragment_room_center

    override fun initView() {

        userRoom = arguments!!.getSerializable(Constants.BUNDLE_T0_USER_ROOM_DATA)
                as UserRoom

        tv_name.text = userRoom!!.nickname
        tv_room_name.text = userRoom!!.account
        tv_nike_name.text = Constants.ROOM_NICKNAME

        adapter = RoomUserAdapter(activity!!, mutableListOf())
        adapter!!.setOnItemClickListener { v, _ ->
            mOnClickRoomListener!!.onClickUserAdd(v,userRooms)
        }
        recycler_view.layoutManager = GridLayoutManager(activity!!, 5)
        recycler_view.adapter = adapter

        tv_room_user_total.setOnClickListener {
            mOnClickRoomListener!!.onClickUserTotal(it, userRooms as ArrayList<UserRoom>)
        }
        tv_room_send_chat.setOnClickListener{
            mOnClickRoomListener!!.onClickSendChat(it, userRoom!!)
        }
        tv_room_clear_chat.setOnClickListener{
            mOnClickRoomListener!!.onClickClearChat(it, userRoom!!)
        }
        tv_room_clear_exit.setOnClickListener{
            mOnClickRoomListener!!.onClickClearExit(it, userRoom!!)
        }


        if (userRoom!!.sub == Constants.USER_ROOM_SUB_4) {
            cl_room_owner.visibility = View.GONE
        } else {
            cl_room_owner.visibility = View.VISIBLE
            mOnClickRoomListener!!.onInitAffiliate(userRoom!!)
        }

    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {

    }

    override fun onRoomOwners(owners: MutableList<UserRoom>) {
        this.userRooms = owners
        tv_room_member_count.text = "共${owners.size}人"
        adapter!!.addData(owners.subList(0, if (owners.size >= 4) 4 else owners.size))
    }
}